﻿@using System.Linq
@using Main.Core.Entities.SubEntities
@using WB.Core.BoundedContexts.Designer.Resources
@using WB.Core.BoundedContexts.Designer.Views.Questionnaire.Pdf
@using WB.Core.SharedKernels.Questionnaire.Documents
@using WB.UI.Designer.Utils
@model Main.Core.Entities.SubEntities.IQuestion

@functions {

    private static List<Answer> GetOptions(ICategoricalQuestion? question, PdfQuestionnaireModel questionnaire)
    {
        if(question == null)
            return new List<Answer>();
        if (!question.CategoriesId.HasValue)
            return question.Answers;

        var categories = questionnaire.CategoriesList.Find(x => x.Id == question.CategoriesId.Value);

        return categories!= null
            ? categories.Items.Select(x => new Answer
            {
                AnswerValue = x.Id.ToString(),
                ParentValue = x.ParentId?.ToString(),
                AnswerText = x.Text
            }).ToList()
            : new List<Answer>();
        
    }

    private static string GetOptionsRef(PdfQuestionnaireModel questionnaire, ICategoricalQuestion? question)
        => question == null? "" : questionnaire.GetOptionsRef(question.CategoriesId.HasValue ? question.CategoriesId.Value : question.PublicKey);
}
@{
    PdfQuestionnaireModel questionnaire = ViewBag.Questionnaire;
    var question = Model!;
}
<div class="question-container">
    <div class="question">
        <div class="question-title" id="@questionnaire.GetItemRef(question.PublicKey)">@Html.Raw(questionnaire.GetQuestionTitle(question))</div>
        <div class="common-info">
            @if (questionnaire.QuestionHasInstructions(question))
            {
                <div class="instructions">
                    <span>I</span>@questionnaire.GetQuestionInstructionExcerpt(question)
                    @if (questionnaire.InstructionIsTooLong(question))
                    {
                        <div class="more">
                            <a href="#@questionnaire.GetInstructionsRef(question.PublicKey)">@Pdf.Format(PdfStrings.AppendixLink, (question.Instructions ?? "").Length - questionnaire.Settings.InstructionsExcerptLength, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "I"))</a>
                        </div>
                    }
                </div>
            }
            @if (questionnaire.QuestionHasOptionsFilter(question))
            {
                <div class="instructions">
                    <span>F</span>@questionnaire.GetQuestionOptionsFilterExcerpt(question)
                    @if (questionnaire.OptionsFilterIsTooLong(question))
                    {
                        <div class="more">
                            <a href="#@questionnaire.GetOptionsFilterRef(question.PublicKey)">
                                @Pdf.Format(PdfStrings.AppendixLink, (questionnaire.GetQuestionOptionsFilter(question) ?? "").Length - questionnaire.Settings.VariableExpressionExcerptLength, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "F"))
                            </a>
                        </div>
                    }
                </div>
            }

            @if (questionnaire.QuestionHasEnablementCondition(question))
            {
                <div class="condition"><span>E</span>@questionnaire.GetExpressionExcerpt(question.ConditionExpression)
                    @if (questionnaire.ExpressionIsTooLong(question.ConditionExpression))
                    {
                        <div class="more">
                            <a href="#@questionnaire.GetConditionRef(question.PublicKey)">
                                @Pdf.Format(PdfStrings.AppendixLink, question.ConditionExpression.Length - questionnaire.Settings.ExpressionExcerptLength, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "E"))
                            </a>
                        </div>
                    }
                </div>
            }

            @for (int i = 0; i < questionnaire.GetValidationsCount(question.ValidationConditions); i++)
            {
                var validation = question.ValidationConditions[i];
                <div class="validation-expression"><span>@(validation.Severity == ValidationSeverity.Warning ?"W":"V")@(i + 1)</span>@questionnaire.GetExpressionExcerpt(validation.Expression)
                    @if (questionnaire.ExpressionIsTooLong(validation.Expression))
                    {
                        <div class="more">
                            <a href="#@questionnaire.GetValidationsRef(question.PublicKey)">
                                @Pdf.Format(PdfStrings.AppendixLink, validation.Expression.Length - questionnaire.Settings.ExpressionExcerptLength, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "V"))
                            </a>
                        </div>
                    }
                </div>
                if (!string.IsNullOrWhiteSpace(@validation.Message))
                {
                    <div class="validation-message"><span>M@(i + 1)</span>@validation.Message</div>
                }
            }
        </div>
    </div>
    <div class="answer">
        <div class="question-meta">
            <div class="type">
                @questionnaire.GetFormattedQuestionType(question)
                @if (question.LinkedToQuestionId.HasValue || question.LinkedToRosterId.HasValue)
                {
                    <br />
                    <span>@PdfStrings.SourceOfCategories</span>
                    @if (question.LinkedToRosterId.HasValue)
                    {
                        <span>@PdfStrings.RosterSourceOfCategories</span>
                        <a href="#@questionnaire.GetItemRef(question.LinkedToRosterId.Value)">@questionnaire.GetVariable(question.LinkedToRosterId.Value)</a>
                    }
                    else if (question.LinkedToQuestionId.HasValue)
                    {
                        @if (questionnaire.GetQuestionType(question.LinkedToQuestionId.Value) == QuestionType.TextList)
                        {
                            <span>@PdfStrings.TextListSourceOfCategories</span>
                            <a href="#@questionnaire.GetItemRef(question.LinkedToQuestionId.Value)">@questionnaire.GetVariable(question.LinkedToQuestionId.Value)</a>
                        }
                        else
                        {
                            <span>@PdfStrings.QuestionInRosterSourceOfCategories</span>
                            <a href="#@questionnaire.GetItemRef(question.LinkedToQuestionId.Value)">@questionnaire.GetVariable(question.LinkedToQuestionId.Value)</a>
                            {
                                var linkedParentRoster = questionnaire.GetParentRoster(question.LinkedToQuestionId.Value);
                                if (linkedParentRoster.HasValue)
                                {
                                    <span>@PdfStrings.InRosterSourceOfCategories</span>
                                    <a href="#@questionnaire.GetItemRef(linkedParentRoster.Value)">@questionnaire.GetVariable(linkedParentRoster.Value)</a>
                                }
                            }
                        }
                    }
                    
                }
                @if (!questionnaire.IsInterviewerQuestion(question) && !questionnaire.IsParentCover(question))
                {
                    <br />
                    <span>@string.Format(PdfStrings.Scope, questionnaire.GetQuestionScope(question))</span>
                }
            </div>
            <div class="variable_name">@question.StataExportCaption</div>
        </div>

        @switch (question.QuestionType)
        {
            case QuestionType.SingleOption:
                <div class="answer-editor">
                    @{
                var singleQuestion = question as ICategoricalQuestion;
                var singleCategories = GetOptions(singleQuestion, questionnaire);
                foreach (var option in singleCategories.Take(questionnaire.Settings.OptionsExcerptCount))
                {
                    <div class="option">
                        <div class="option-value"><span >@questionnaire.GetFormattedOptionValue(singleCategories, @option.AnswerValue)</span></div>
                        <div class="option-text">
                            <input type="radio" id="@question.StataExportCaption-@option.AnswerValue" name="@question.StataExportCaption">
                            <label for="@question.StataExportCaption-@option.AnswerValue">@option.AnswerText</label>
                        </div>
                        <div class="option-attachment-name">
                            <span>@option.AttachmentName</span>
                        </div>
                    </div>
                }
                @if (singleCategories.Count > questionnaire.Settings.OptionsExcerptCount)
                {
                    <div class="more">
                        <a href="#@GetOptionsRef(questionnaire, singleQuestion)">
                            @Pdf.Format(PdfStrings.AppendixLink, singleCategories.Count - questionnaire.Settings.OptionsExcerptCount, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "O"))
                        </a>
                    </div>
                }}
                </div>
                break;
            case QuestionType.MultyOption:
                <div class="answer-editor multi-option">
                    @{
                var multiQuestion = question as ICategoricalQuestion;
                var multiCategories = GetOptions(multiQuestion, questionnaire);

                foreach (var option in multiCategories.Take(questionnaire.Settings.OptionsExcerptCount))
                {
                    <div class="option">
                        <div class="option-value">
                            @questionnaire.GetFormattedOptionValue(multiCategories, @option.AnswerValue)
                        </div>
                        @if (questionnaire.IsYesNoMultiQuestion(question))
                        {
                            <div class="yes_no_multiselect">
                                <input type="radio" id="@question.StataExportCaption-@option.AnswerValue-yes" name="@question.StataExportCaption-@option.AnswerValue">
                                <label for="@question.StataExportCaption-@option.AnswerValue-yes"></label>
                                <span style="vertical-align: top;">/</span>
                                <input type="radio" id="@question.StataExportCaption-@option.AnswerValue-no" name="@question.StataExportCaption-@option.AnswerValue">
                                <label for="@question.StataExportCaption-@option.AnswerValue-no"></label>
                            </div>
                            <div class="yes_no-option-text">@option.AnswerText</div>
                            <div class="option-attachment-name">
                                <span>@option.AttachmentName</span>
                            </div>
                        }
                        else
                        {
                            <div class="option-text">
                                <input type="checkbox" id="@question.StataExportCaption-@option.AnswerValue" name="@question.StataExportCaption">
                                <label for="@question.StataExportCaption-@option.AnswerValue">@option.AnswerText</label>
                            </div>
                            <div class="option-attachment-name">
                                <span>@option.AttachmentName</span>
                            </div>
                        }
                    </div>
                }
                @if (multiCategories.Count > questionnaire.Settings.OptionsExcerptCount)
                {
                    <div class="more">
                        <a href="#@GetOptionsRef(questionnaire, multiQuestion)">
                            @Pdf.Format(PdfStrings.AppendixLink, multiCategories.Count - questionnaire.Settings.OptionsExcerptCount, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "O"))
                        </a>
                    </div>
                }}
                </div>
                break;
            case QuestionType.Numeric:
                <div class="answer-editor">
                    <input type="number" class="dashes">
                </div>
                if (question.Answers.Count > 0)
                {
                    <div class="question-meta more">
                        <div class="type">
                            @PdfStrings.SpecialValues
                        </div>
                    </div>
                    <div class="question-meta">

                        @foreach (var option in question.Answers.Take(questionnaire.Settings.OptionsExcerptCount))
                        {
                            <div class="option">
                                <div class="option-value"><span>@questionnaire.GetFormattedOptionValue(question.Answers, @option.AnswerValue)</span></div>
                                <div class="option-text">
                                    <label for="@question.StataExportCaption-@option.AnswerValue">@option.AnswerText</label>
                                </div>
                                <div class="option-attachment-name">
                                    <span>@option.AttachmentName</span>
                                </div>
                            </div>
                        }
                        @if (question.Answers.Count > questionnaire.Settings.OptionsExcerptCount)
                        {
                            <div class="more">
                                <a href="#@questionnaire.GetOptionsRef(question.PublicKey)">
                                    @Pdf.Format(PdfStrings.AppendixLink, question.Answers.Count - questionnaire.Settings.OptionsExcerptCount, questionnaire.GetEntityIndexInAppendix(question.PublicKey, "SV"))
                                </a>
                            </div>
                        }
                    </div>
                }
                break;
            case QuestionType.DateTime:
                <div class="answer-editor">
                    <input type="date" class="points">
                </div>
                break;
            case QuestionType.GpsCoordinates:
                <div class="answer-editor gps">
                    <input type="text" class="dashes">
                    <sup>N</sup>
                    <input type="text" class="dashes">
                    <sup>W</sup>
                    <input type="text" class="dashes">
                    <sup>A</sup>
                </div>
                break;
            case QuestionType.Text:
                <div class="answer-editor">
                    <input type="text" class="points">
                </div>
                break;
            case QuestionType.TextList:
                <div class="answer-editor">
                    <input type="text" class="points">
                </div>
                break;
            case QuestionType.QRBarcode:
                <div class="answer-editor">
                </div>
                break;
            case QuestionType.Multimedia:
                <div class="answer-editor">
                </div>
                break;
            case QuestionType.Area:
                <div class="answer-editor">
                </div>
                <div class="question-meta more">
                    <div class="type">
                        @PdfStrings.GeographyInputMode : <span>@(question.Properties?.GeometryInputMode ?? GeometryInputMode.Manual)</span>
                    </div>
                </div>
                <div class="question-meta more">
                    <div class="type">
                        @PdfStrings.GeometryOverlapDetection : <span>@(question.Properties?.GeometryOverlapDetection == true? PdfStrings.On: PdfStrings.Off )</span> 
                    </div>
                </div>
                break;
            case QuestionType.Audio:
                <div class="answer-editor">
                </div>
                break;
        }
    </div>
</div>
